package com.visionet.waveletData.web.repository.business.specs;

import com.visionet.waveletData.web.entity.account.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

public class UserInfoSearchSpecs {
	public static Specification<User> searchTeamByCondition(final User userInfo) {
		return new Specification<User>(){

			@Override
			public Predicate toPredicate(Root<User> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				
				if(userInfo.getId()!=null){
					list.add(cb.equal(root.<Long>get("id"), userInfo.getId()));
				}
				String userName = StringUtils.trimToEmpty(userInfo.getUser_name());
				if(StringUtils.isNotEmpty(userInfo.getUser_name())) {
					list.add(cb.like(root.<String>get("user_name"), "%"+userInfo.getUser_name()+"%"));
				}

				list.add(cb.equal(root.<String>get("is_deleted"), "0"));
				list.add(cb.equal(root.<String>get("company_id"), userInfo.getCompany_id()));
				
//				String firstLetter = StringUtils.trimToEmpty(userInfo.getFirstLetter());
//				if(!userName.isEmpty()&&!firstLetter.isEmpty()){
//					list.add(cb.or(
//							cb.like(root.<String>get("userName"), "%"+userInfo.getUserName()+"%"),
//							cb.like(root.<String>get("firstLetter"), userInfo.getFirstLetter()+"%"))
//					);
//				}else if(!userName.isEmpty()){
//					list.add(cb.like(root.<String>get("userName"), "%"+userInfo.getUserName()+"%"));
//				}else if(!firstLetter.isEmpty()){
//					list.add(cb.like(root.<String>get("firstLetter"), userInfo.getFirstLetter()+"%"));
//				}
//				
//				if(userInfo.getPhoneNumber()!=null){
//					list.add(cb.equal(root.<Long>get("phoneNumber"), userInfo.getPhoneNumber()));
//				}
//				if(userInfo.getLoginName()!=null){
//					list.add(cb.equal(root.<String>get("loginName"), userInfo.getLoginName()));
//				}
//
//                String chexiangQuery = userInfo.getChexiangAccount();
//                if(chexiangQuery!=null){
//                    if(!chexiangQuery.isEmpty()) {
//                        list.add(cb.or(
//                                        cb.like(root.<String>get("userName"), "%" + chexiangQuery + "%"),
//                                        cb.like(root.<String>get("trueName"), "%" + chexiangQuery + "%"),
//                                        cb.like(root.<String>get("loginName"), "%" + chexiangQuery + "%"),
//                                        cb.like(root.<String>get("chexiangAccount"), "%" + chexiangQuery + "%")
//                                )
//                        );
//                    }
//                    list.add(cb.isNotNull(root.get("chexiangAccount")));
//                }
//
//				if(userInfo.getUpdateDate()!=null){
//					list.add(cb.greaterThanOrEqualTo(root.<Date>get("updateDate"), userInfo.getUpdateDate()));
//				}
//				list.add(cb.equal(root.<Long>get("orgId"), userInfo.getOrgId()));
//				//过滤查询管理员
//				list.add(cb.notEqual(root.<Long>get("id"),1l));
				 Predicate[] predicate = new Predicate[list.size()];
		    	 return cb.and(list.toArray(predicate));
			}
		};
		
	}

}
